﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.BeamNG.VehiclePartConfig;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.Controls
{
    public partial class NodeSelectionControl2 : VehiclePartControl
    {
        #region Fields

        public event EventHandler<Node> NodeValidated;
        public event EventHandler<Node> SelectedNodeChanged;

        #endregion

        #region Properties

        public Node SelectedNode
        {
            get { return nodesBindingSource.Current as Node; }
            set { listBox.SelectedItem = value; }
        }

        #endregion

        #region CTORs

        public NodeSelectionControl2()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedNodeChanged != null)
                SelectedNodeChanged(this, SelectedNode);
        }

        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            if (NodeValidated != null)
                NodeValidated(this, SelectedNode);
        }

        #endregion

        #region Methods

        private void BindNodes()
        {
            nodesBindingSource.DataSource = null;
            nodesBindingSource.DataSource = VehiclePart != null ? VehiclePart.NodeSection.ElementCollection : null;
            nodesBindingSource.ResetBindings(true);
        }

        protected override void OnVehiclePartChanged()
        {
            BindNodes();
            base.OnVehiclePartChanged();
        }

        #endregion
    }
}
